/**
 * Created by Eldred Brown on 2015-05-07
 */
var SetOpenPositionURL = "http://140.211.68.130/SetOpenPosition";

var request = {"position_type_id": ""};

CM = {};
CM.ret_val = {};
CM.ret_val.positions = {};
CM.position_count = 0;
CM.server_data = {};
CM.server_data.params = {};
CM.server_data.params.method = 'update';

$form = $('<form id="master_form"></form>');

function getUrlParameter(sParam) {
    var sPageURL = window.location.search.substring(1);
    var sURLVariables = sPageURL.split('&');
    for (var i = 0; i < sURLVariables.length; i++) {
        var sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] == sParam) {
            return sParameterName[1];
        }
    }
    return undefined;
}

function BuildForm() {
    var positions = CM.ret_val.positions === undefined ?
        [] :
        CM.ret_val.positions;

    $form.append('<h1>Request Positions</h1>');

    $form.append('<table style="width:100%" bgcolor="#F6F6FF">');
    $form.append('<tr>');
    $form.append('<th>Position ID</th>');
    $form.append('<th>Title</th>');
    $form.append('<th>Description</th>');
    $form.append('<th>Store ID</th>');
    $form.append('<th>Open?</th>');
    $form.append('<th>Count</th>');
    $form.append('</tr>');

    var position_count = 0;
    for (var i = 0; i < positions.length; i++) {
        CM.position_count = i;

        $form.append('<tr>');
        $form.append('<td>' + CM.ret_val.positions[i]['position_type_id'] + '</td>');
        $form.append('<td>' + CM.ret_val.positions[i]['title'] + '</td>');
        $form.append('<td>' + CM.ret_val.positions[i]['description'] + '</td>');
        $form.append('<td>' + CM.ret_val.positions[i]['store_id'] + '</td>');
        if (CM.ret_val.positions[i]['is_open'] == 1) {
            $form.append(
                '<td><input type="checkbox" id="position_open" name="position' + i + '_open" ' +
                'class="position' + i + '_open" checked="checked" /></td>'
            );
        }
        else {
            $form.append(
                '<td><input type="checkbox" id="position_open" name="position' + i + '_open" ' +
                'class="position' + i + '_open" /></td>'
            );
        }
        $form.append(
            '<td><input type="text" id="num_openings" name="position' + i + '_num_openings" ' +
            'class="position' + i + '_num_openings" value="' + CM.ret_val.positions[i]['job_count'] + '" /></td>'
        );
        $form.append('</tr>');
    }
    $form.append("</table");

    $form.append("<br>");

    $form.append('<div align="center">');
    $form.append('<input type="submit" id="send_server" value="Submit Request" />');
    $form.append('</div>');
    $form.append('<br>');
    $form.append('<br>');

    $('#form_holder').append($form);
}

function SendDataToServer() {
    var position_count = 0;

    for (i = 0; i < CM.ret_val.positions.length; i++) {
        CM.server_data = {};
        CM.server_data.params = {};

        CM.server_data.params.posting_id = null;
        CM.server_data.params.position_type_id = CM.ret_val.positions[i]['position_type_id'].trim();
        CM.server_data.params.start_date = '2016-01-01';
        CM.server_data.params.end_date = '2016-12-31';
        CM.server_data.params.store_id = CM.ret_val.positions[i]['store_id'].trim();

        var num_openings = CM.form_data['position' + i + '_num_openings'].trim();
        CM.server_data.params.job_count = num_openings.length == 0 ? '0' : num_openings;

        var success = '';
        CM.server_data.params = JSON.stringify(CM.server_data);

        $.ajax({
            type: 'POST',
            url: SetOpenPositionURL,
            data: CM.server_data,
            async: false,
            success: function(data) {
                success = data;
            },
            dataType: 'json'
        });

        success;
    }

    location.reload();
}
